package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;

/**
 * @author Fengkai  乱序整数序列 两数之和绝对值最小
 * @creat 2023/3/29
 */
public class GetMinAbs {

    public static void main (String[] args) throws IOException {
//        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        String[] s = br.readLine().split(" ");
//        int[] ints = new int[s.length];
//        for (int i = 0; i < ints.length; i++) {
//            ints[i] = Integer.parseInt(s[i]);
//        }
        for (int i = 0; i < 20; i++) {
            int[] randomInts = new int[(int) (Math.random() * 1000 + 1)];
            for (int j = 0; j < randomInts.length; j++) {
                randomInts[j] = (int) (Math.random() * 65535 + 1) - (int) (Math.random() * 65535 + 1);
                if (!(mySlution(randomInts) == ovjectSlution(randomInts))) {
                    System.out.println("出错啦！");
                }
            }

        }


    }

    public static int mySlution (int[] ints) {
        int sum = 65535 * 2;
        int [] out = new int[2];
        for (int i = 0; i < ints.length - 1; i++) {
            for (int j = i + 1; j < ints.length; j++) {
                if (sum > Math.abs(ints[i] + ints[j])) {
                    sum = Math.abs(ints[i] + ints[j]);
                    out[0] = ints[i];
                    out[1] = ints[j];
                }
            }
        }
        return sum;
    }

    public  static int ovjectSlution(int [] ints){
        int sum = 65535 * 2;
        int[] indexArray = new int[2];
        ArrayList<Integer> dataList = new ArrayList<>();

        for (int s : ints) {
            dataList.add(Integer.valueOf(s));
        }
        dataList.sort(Comparator.naturalOrder());
        for (int i = 0; i < dataList.size(); ++i) {
            for (int j = i + 1; j < dataList.size(); ++j) {
                int disValue = Math.abs(dataList.get(i) + dataList.get(j));
                if (disValue <= sum) {
                    sum = disValue;
                    indexArray[0] = i;
                    indexArray[1] = j;
                }
            }
        }
            return sum;
    }

}
